package com.lyc.assistant.translate.adapter;


import android.view.View;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lyc.assistant.R;
import com.translation.assistant.data.bean.Language;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * Desc: 离线语言翻译包
 */
public class LanguagePackageAdapter extends BaseQuickAdapter<Language, BaseViewHolder> {

    private HashMap<String, String> downloadMap;
    private ArrayList<String> downloadedModelList;

    public LanguagePackageAdapter(@Nullable List<Language> data, ArrayList<String> models,
                                  HashMap<String, String> map) {
        super(R.layout.item_language_packe, data);
        downloadedModelList = models;
        downloadMap = map;
        addChildClickViewIds(R.id.rl_parent);
        addChildClickViewIds(R.id.tv_download);
    }

    @Override
    protected void convert(BaseViewHolder helper, Language item) {

        if (item != null) {
            if(item.getCode().equals("en")){
                helper.getView(R.id.tv_download).setVisibility(View.GONE);
            }else {
                helper.getView(R.id.tv_download).setVisibility(View.VISIBLE);
            }
            if (downloadedModelList.contains(item.getCode())) {
                //已下载
                helper.setText(R.id.tv_download,R.string.offline_delete);
                helper.setTextColor(R.id.tv_download,getContext().getResources().getColor(R.color.color_282A43));
                helper.setBackgroundResource(R.id.tv_download,R.drawable.shape_stroke_9da9b9_x8);
            }else {
                //未下载、正在下载
                helper.setText(R.id.tv_download,R.string.download);
                helper.setTextColor(R.id.tv_download,getContext().getResources().getColor(R.color.white));
                helper.setBackgroundResource(R.id.tv_download,R.drawable.shape_corner_bule_x8);
            }
            String itemTitle = String.format(Locale.getDefault(),
                    getContext().getResources().getString(R.string.language_package_str), item.getName());
            helper.setText(R.id.tv_language_name,itemTitle);
            /*String size = String.format(Locale.getDefault(),
                    getContext().getResources().getString(R.string.language_package_size_str), (30-new Random().nextInt(5)));
            helper.setText(R.id.tv_size, size);*/

            String progress =  downloadMap.get(item.getCode());
            if(progress != null){
                helper.setText(R.id.tv_download, progress);
                helper.setTextColor(R.id.tv_download,getContext().getResources().getColor(R.color.primary_color));
                helper.setBackgroundResource(R.id.tv_download,R.drawable.shape_stroke_blue_x8);
            }
        }
    }

}
